﻿package core.object
{
	import flash.display.Sprite;
	import flash.events.Event;
	import core.Config;

	
	public class DropObject extends Sprite
	{
		private var _id:int;
		
		private var animation:GameAnimation;
		
		public function DropObject(id:int)
		{
			this._id = id;
			animation = new GameAnimation(id,false);
		}
		
		public function get id():int {
			return this._id;
		}
		
		public var ySpeed:Number = 5;
		public var xSpeed:Number = 5;
		public var rSpeed:Number = 25;
		
		public function drop():void {
			this.xSpeed = Math.max(5,Math.random() * 20) * (Math.random() > 0.5 ? 1 : -1);
			this.ySpeed = Math.max(5,Math.random() * 15);
			this.animation.x = -this.animation.width / 2;
			this.animation.y = -this.animation.height / 2;
			this.addChild(animation);
			this.addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
		}
		
		private function onEnterFrameHandler(event:Event):void {
			this.x += this.xSpeed;
			this.y += this.ySpeed;
			this.rotation += this.rSpeed;
			this.bounce();
		}
		
		protected function bounce():void {
			if(this.x < 0){
				this.x = 0;
				this.xSpeed *= -1;
			}else if(this.x > Config.WIDTH){
				this.x = Config.WIDTH;
				this.xSpeed *= -1;
			}
			
			if(this.y > Config.HEIGHT + this.height){
				this.dispose()
			}
		}
		
		public function dispose():void {
			this.removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
			this.animation.dispose();	
		}
		
		/**
		 * TODO 代码要优化
		 * 判断是否是property,如果是property的话,那么掉落之后就执行效果
		 */ 
		public function get isProperty():Boolean {
			return this.animation.sheet.data.hasOwnProperty("isProperty");
		}
		
		/**
		 * 是否立即执行
		 */ 
		public function get isImmediately():Boolean {
			return this.animation.sheet.data.hasOwnProperty("isImmediately");
		}
		
		public function get isNormal():Boolean {
			return this.animation.sheet.data.hasOwnProperty("isNormal");
		}
	}
}